package com.bsoft.gol.editor.controller;


import ctd.controller.exception.ControllerException;
import ctd.util.context.ContextUtils;
import hcn.util.BaseRespUtil;
import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.EditorAuthority;
import com.bsoft.gol.editor.editor.EditorQuestionnaire;
import com.bsoft.gol.editor.editor.qo.AuthorityRemoveQo;
import com.bsoft.gol.editor.editor.qo.EditorQuestionnaireQo;
import com.bsoft.gol.editor.editor.qo.UpdatetypeQo;
import com.bsoft.gol.editor.editor.request.QuestionReq;
import com.bsoft.gol.editor.editor.qo.EditorAuthorityQo;
import com.bsoft.gol.editor.editor.response.EditorQuestionnaireDetailRes;
import com.bsoft.gol.editor.editor.response.QueryEditorQuestionnaireListResponse;
import hcn.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bsoft.gol.editor.service.EditorQuestionnaireApi;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * @author lxy
 * @date 2019/6/18
 */
@Slf4j
@Controller
@RequestMapping(value = "/api/auth/editorQuestionnaire")
public class EditorQuestionnaireController {

    @Autowired
    private EditorQuestionnaireApi editorQuestionnaireApi;


    @RequestMapping("/list")
    @ResponseBody
    public BaseResponse<QueryEditorQuestionnaireListResponse> list(@RequestBody EditorQuestionnaireQo req) throws ControllerException {
        if(ContextUtils.get("$ur")==null){
            return BaseRespUtil.setResultError("无权限");
        }
        log.info("editorQuestionnaire:$ur,{}", ContextUtils.get("$ur"));
        BaseResponse<QueryEditorQuestionnaireListResponse> baseResponse = editorQuestionnaireApi.pageList(req);
        return baseResponse;
    }

    @RequestMapping("/updatetype")
    @ResponseBody
    public BaseResponse updatetype(@RequestBody UpdatetypeQo req) {

        BaseResponse baseResponse = new BaseResponse();
        if (CommonUtils.isNotEmpty(req.getQuestionnaireId()) && CommonUtils.isNotEmpty(req.getType())) {
            baseResponse = editorQuestionnaireApi.updatetype(req);
        } else {
            baseResponse.setCode(500);
            baseResponse.setMessage("问卷id不能为空/问卷类型不能为空");
        }
        return baseResponse;
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse<String> add(@RequestBody EditorQuestionnaire req) throws ControllerException {

        BaseResponse<String> response = new BaseResponse<>();
        String questionnaireId = editorQuestionnaireApi.addQuestionnaire(req);

        response.setData(questionnaireId);

        response.setMessage("保存成功");

        return response;

    }

    //
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse<String> remove(@RequestBody QuestionReq req) {

        BaseResponse<String> response = new BaseResponse<>();
        editorQuestionnaireApi.removeQuestionnaire(req.getQuestionnaireId());

        response.setData(req.getQuestionnaireId());

        response.setMessage("删除成功");

        return response;
    }

    //
    @RequestMapping("/copy")
    @ResponseBody
    public BaseResponse<String> copyQuestion(@RequestBody EditorQuestionnaire req) {

        BaseResponse<String> response = new BaseResponse<>();

        if (StringUtils.isNotEmpty(req.getQuestionnaireId()) && StringUtils.isNotEmpty(req.getQuestionnaireName())) {
            String questionnaireId = editorQuestionnaireApi.copyQuestion(req);

            response.setData(questionnaireId);

            response.setMessage("复制成功");

        } else {
            response.setCode(500);
            response.setMessage("参数缺失");
        }

        return response;
    }

    //
    @RequestMapping("/state")
    @ResponseBody
    public BaseResponse<String> editState(@RequestBody QuestionReq req) {

        BaseResponse<String> response = new BaseResponse<>();

        if (CommonUtils.isNotEmpty(req.getQuestionnaireId()) && CommonUtils.isNotEmpty(req.getQuestionnaireState())) {

            editorQuestionnaireApi.editState(req.getQuestionnaireId(), req.getQuestionnaireState());

            response.setMessage("状态更新成功");

        } else {
            response.setCode(500);
            response.setMessage("参数缺失");
        }

        return response;
    }
//

    /**
     * 包含更新版本的逻辑
     *
     * @param req req
     * @return detail
     */
    @RequestMapping("/detail")
    @ResponseBody
    public BaseResponse<EditorQuestionnaireDetailRes> detail(@RequestBody QuestionReq req) {

        BaseResponse<EditorQuestionnaireDetailRes> response = new BaseResponse<>();

        if (CommonUtils.isNotEmpty(req.getQuestionnaireId())) {

            EditorQuestionnaireDetailRes detail = editorQuestionnaireApi.detail(req.getQuestionnaireId());

            response.setData(detail);

            response.setMessage("success");

        } else {
            response.setCode(500);
            response.setMessage("参数缺失");
        }

        return response;
    }

    @RequestMapping("/authorityList")
    @ResponseBody
    public BaseResponse<HashMap<String, ArrayList<EditorAuthority>>> authorityList(@RequestBody EditorAuthority req) {
        BaseResponse<HashMap<String, ArrayList<EditorAuthority>>> response = new BaseResponse<>();
        if (CommonUtils.isNull(req.getQuestionnaireId())) {
            response.setCode(ResultCode.PARAMETERS_EMPTY);
            response.setMessage("传入参数不能为空");
            return response;
        }
        HashMap<String, ArrayList<EditorAuthority>> map = editorQuestionnaireApi.authorityList(req);
        response.setData(map);
        return response;
    }

    @RequestMapping("/authorityAdd")
    @ResponseBody
    public BaseResponse authorityList(@RequestBody EditorAuthorityQo req) {
        BaseResponse<EditorAuthorityQo> response = new BaseResponse<>();
        response = editorQuestionnaireApi.authorityAdd(req);
        return response;
    }

    @RequestMapping("/authorityRemove")
    @ResponseBody
    public BaseResponse authorityRemove(@RequestBody AuthorityRemoveQo req) {
        BaseResponse<EditorAuthorityQo> response = new BaseResponse<>();
        editorQuestionnaireApi.authorityRemove(req);
        return response;
    }

}
